/**
@author		Jong Min, Lee
@date		  01/21/2014
@breif		system environments

copyright(c) Tae Young Soft. All right reserved. 2014
*/
#include "stdafx.h"
#include "Taurus_Common/SystemEnvironment.h"

#include "Taurus_Util/Macros.h"

#define PROPERTY_PATH _T("Property\\")
#define STUDY_PATH _T("Study\\")
#define LOGGER_PATH _T("Logger\\")

CSystemEnvironment* CSystemEnvironment::m_pInstance = NULL;

CSystemEnvironment::CSystemEnvironment()
{}

CSystemEnvironment::~CSystemEnvironment()
{}

CSystemEnvironment* CSystemEnvironment::getInstance()
{
  if(m_pInstance == NULL)
    _TRY_ALLOC(m_pInstance, CSystemEnvironment);

  return m_pInstance;
}

void CSystemEnvironment::createEnvionment()
{
  TCHAR szBuffer[MAX_PATH];
  GetModuleFileName(NULL, szBuffer, MAX_PATH);
  m_strAppRootPath = szBuffer;
  int iPos;
  if((iPos = m_strAppRootPath.ReverseFind(_T('\\'))) > 0)
    m_strAppRootPath = m_strAppRootPath.Left(iPos + 1);

  m_strPropertyPath = makePropertyPath(m_strAppRootPath);
  m_strStudyPath = makeStudyListPath(m_strAppRootPath);
  m_strLoggerCfgPath = makeLoggerCfgPath(m_strAppRootPath);
}

const TCHAR* CSystemEnvironment::getAppRootPath() const
{
  return m_strAppRootPath;
}

const TCHAR* CSystemEnvironment::getPropertyPath() const
{
  return m_strPropertyPath;
}

const TCHAR* CSystemEnvironment::getStudyListPath() const
{
  return m_strStudyPath;
}

const TCHAR* CSystemEnvironment::getLoggerCfgPath() const
{
  return m_strLoggerCfgPath;
}

CString CSystemEnvironment::makePropertyPath(const TCHAR* pszRootPath) const
{
  CString strProfile = pszRootPath;

  while(strProfile.Right(1) == _T("\\"))
  {
    strProfile = strProfile.Left(strProfile.GetLength() - 1);
  }

  while(strProfile.Right(1) != _T("\\"))
  {
    strProfile = strProfile.Left(strProfile.GetLength() - 1);
  }

  strProfile += PROPERTY_PATH;

  CFileFind ff;
  if(!ff.FindFile(strProfile)) CreateDirectory(strProfile, NULL);
  ff.Close();
  return strProfile;
}

CString CSystemEnvironment::makeStudyListPath(const TCHAR* pszRootPath) const
{
  CString strStudy = pszRootPath;

  while(strStudy.Right(1) == _T("\\"))
  {
    strStudy = strStudy.Left(strStudy.GetLength() - 1);
  }

  while(strStudy.Right(1) != _T("\\"))
  {
    strStudy = strStudy.Left(strStudy.GetLength() - 1);
  }

  strStudy += STUDY_PATH;

  CFileFind ff;
  if(!ff.FindFile(strStudy)) CreateDirectory(strStudy, NULL);
  ff.Close();
  return strStudy;
}

CString CSystemEnvironment::makeLoggerCfgPath(const TCHAR* pszRootPath) const
{
  CString strPath = pszRootPath;

  while(strPath.Right(1) == _T("\\"))
  {
    strPath = strPath.Left(strPath.GetLength() - 1);
  }

  while(strPath.Right(1) != _T("\\"))
  {
    strPath = strPath.Left(strPath.GetLength() - 1);
  }

  strPath += LOGGER_PATH;

  CFileFind ff;
  if(!ff.FindFile(strPath)) CreateDirectory(strPath, NULL);
  ff.Close();
  return strPath;
}
